package search;

public class BinarySearch {


    public static void main(String[] args) {
        String str = "abcdef";
        str.toLowerCase();
        String news = str.concat("abc");
        System.out.println(str + "   "+news);
    }

    /*
    * 二分查找 递归  只适用于有序数组
    * */
    public static int search(int[] nums,int n,int begin, int end){
        int mid = (begin+end)/2;
        System.out.println(mid);
        if (n == nums[mid]){
            return mid;
        }
        if (n < nums[mid] && begin <mid-1){
            return search(nums,n,begin,mid-1);
        }
        if (n > nums[mid] && mid+1 <end){
            return search(nums,n,mid+1,end);
        }
        return 0;
    }


    //二分查找  循环
    public static int binSearch(int[] arr,int target){
        if (arr.length<=1){
            return arr.length;
        }
        int begin = 0;
        int end = arr.length-1;
        while (begin<=end){
            int mid = (begin+end)/2;
            System.out.println(mid);
            if (arr[mid]==target){
                return mid;
            }else if (arr[mid] <target){
                begin = mid + 1;
            }else {
                end = mid-1;
            }
        }
        return -1;
    }
}
